package com.atguigu.d_threadstart3;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //匿名内部类完成接口实现
        //1. 编写线程任务
        //2. 创建FutureTask对象 -> 接收线程的任务
        FutureTask<String> task = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                //编写线程任务
                for (int i = 1; i <= 100; i++) {
                    System.out.println("小明给女神的第 " + i + " 次表白 ! ");
                }
                return "女神说 : 滚 !";
            }
        });

        //3. 创建Thread对象 -> 接收中间键对象
        Thread t = new Thread(task);

        //4. 启动线程
        t.start();


        //main线程的其他任务
        for (int i = 1; i <= 100; i++) {
            System.out.println("打印 " + i + " 次Hello ! ");
        }

        //获取任务结果
        //此句代码是main线程负责执行
        String result = task.get();//必须线程完成任务后拿结果
        System.out.println("result = " + result);

    }
}
